package com.easylinkin.linkappapi.meterbilling.config;

import com.easylinkin.linkappapi.common.constant.CommonConstant;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;
import site.morn.boot.web.Responses;
import site.morn.framework.context.AccountContext;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * class info :
 *
 * @author liuqihang
 * @date 2021/7/30 16:04
 */
@Component
public class WechatPublicAuthenticationSuccessHandler implements AuthenticationSuccessHandler {

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
        // 清空登录缓存
        AccountContext.cacheGroup().clearKey(authentication.getName());
        HttpSession session = request.getSession(false);
        if(session != null){
            session.setMaxInactiveInterval(CommonConstant.SESSION_TIME_DEFAULT);;
        }
        RestMessage message = RestBuilders.successMessage("login.success");

        Responses.standard(httpServletResponse).respond(message);
    }
}
